# libreoffice
# Builds from https://github.com/puppylinux-woof-CE/petbuilds
# File auto generated by ./mk_petbuild.sh. Edit as necessary.

. ../func
. ../build.conf

URL=https://downloadarchive.documentfoundation.org/libreoffice/old/5.2.2.2/src
PKG=libreoffice
VER=5.2.2.2
COMP=tar.xz
DESC="LibreOffice is a full-featured office suite."
DEPS=
CAT=Document
DESKTOP=
BUILD=
# extras thanks to kirk and jamesbond
PKGVER=$VER
DEPS_TARBALL=${PKG}-$PKGVER-external-deps.tar.gz
DEPS_MD5SUM=79ed84a651162af60ec08fde2f8b94bc ###5e0d65018b92e3eadd50d55e1c53c682
DEPS_URL=http://distro.ibiblio.org/fatdog/source/710/$DEPS_TARBALL

HELP_TARBALL=${PKG}-help-$PKGVER.tar.xz
HELP_MD5SUM=ea273836b1e1b670eee6f211bd5d9acf
HELP_URL=https://downloadarchive.documentfoundation.org/libreoffice/old/$PKGVER/src/$HELP_TARBALL

DICT_TARBALL=${PKG}-dictionaries-$PKGVER.tar.xz
DICT_MD5SUM=96e9ffae7c4c3163ebc0896652b4bd51
DICT_URL=https://downloadarchive.documentfoundation.org/libreoffice/old/$PKGVER/src/$DICT_TARBALL


CWD=$(pwd)
[ -z "$MWD" ] && MWD=$(dirname $CWD)
[ -z "$BUILD" ] && BUILD=$DEF_BUILD
# comment these 2 below if you use a custom package() function
#[ -f "$DESKTOP" ] || DESKTOP=not
#[ -z "$DEPS" ] && DEPS=not

ARCH=$(uname -m)
case $ARCH in # set in build.conf
 *64) 	LIBDIR=$LIBCONF64 ;;
 arm*)	LIBDIR=$LIBCONFarm;;
 *) 	LIBDIR=$LIBCONF32 ;;
esac

retrieve_extras() {
	for z in $DEPS_URL $HELP_URL $DICT_URL;do
		pkg=${z##*/}
		if [ -f "$MWD/0sources/$pkg" ];then
			cp -a "$MWD/0sources/$pkg" .
		else
			wget -t0 -c $z
		fi
		CKSUM=`md5sum $pkg`
		echo ${CKSUM% *}
		case $pkg in
			$DEPS_TARBALL)echo $DEPS_MD5SUM
				[ ${CKSUM% *} == $DEPS_MD5SUM ] || return 1
				tar xvf ${pkg#* }
				mv -f $pkg $MWD/0sources/;;
			$HELP_TARBALL)echo $HELP_MD5SUM 
				[ ${CKSUM% *} == $HELP_MD5SUM ] || return 1
				cp -a $pkg $MWD/0sources/;;
			$DICT_TARBALL)echo $DICT_MD5SUM
				[ ${CKSUM% *} == $DICT_MD5SUM ] || return 1
				cp -a $pkg $MWD/0sources/;;
		esac
	done	
}

build() {
	mv ${PKG}-$PKGVER-external-deps/tarballs/ ${PKG}-$PKGVER/external/ &&
	rm -r ${PKG}-$PKGVER-external-deps &&
	cd ${PKG}-$PKGVER/ &&
	mv ../$HELP_TARBALL external/tarballs &&
	mv ../$DICT_TARBALL external/tarballs &&

	# build with -Os
	export CFLAGS="-Os" CXXFLAGS="-Os" &&
	sed -i -e "s/-O2/-Os/" configure.ac &&
	
	# BLFS patches: disable manpage compression & fix install
	sed -e "/gzip -f/d" -e "s|.1.gz|.1|g" -i bin/distro-install-desktop-integration &&
	sed -e "/distro-install-file-lists/d" -i Makefile.in &&
	
	# configure
	./autogen.sh CXXFLAGS=-Os CFLAGS=-Os --prefix=$CWD/${PKG}-install/usr --libdir=$CWD/${PKG}-install/usr/$LIBDIR \
             --sysconfdir=$CWD/${PKG}-install/etc --localstatedir=$CWD/${PKG}-install/var  \
             --with-vendor=Slacko      \
             --with-lang='en-US en-GB pl fr de it es'      \
             --enable-gtk           \
             --disable-gtk3            \
             --disable-dbus           \
             --disable-sdremote         \
             --disable-sdremote-bluetooth  \
             --disable-gio                 \
             --disable-tdeab             \
             --disable-firebird-sdbc    \
             --disable-collada       \
             --disable-scripting-beanshell \
             --disable-scripting-javascript \
             --with-alloc=system         \
             --without-java              \
             --disable-dconf             \
             --disable-odk               \
             --enable-release-build=yes  \
             --enable-python=no     \
             --disable-report-builder   \
             --disable-lpsolve          \
             --disable-coinmp           \
             --disable-online-update     \
             --disable-lotuswordpro      \
             --disable-lpsolve           \
             --disable-postgresql-sdbc   \
             --disable-gltf   		\
             --without-galleries         \
             --with-theme="tango"	\
             --without-fonts		\
             --with-system-cairo         \
             --with-system-apr 			\
             --with-system-curl          \
             --with-system-expat         \
             --with-system-jpeg          \
             --with-system-lcms2         \
             --with-system-libpng        \
             --with-system-libxml        \
             --with-system-neon          \
             --with-system-openssl       \
             --with-system-poppler       \
             --with-system-zlib        \
             --with-system-glew  		 \
             --with-system-nss        \
             --with-tls=openssl 		\
             --enable-mergelibs        \
             --with-system-boost		\
             --disable-gstreamer-1-0   \
             --disable-gstreamer-0-10	\
             --disable-ccache         \
             --with-help              \
             --with-myspell-dicts     \
             --without-system-dicts   \
             --without-sun-templates &&
             ##--without-help                 \
             ##--without-helppack-integration  \
             ##--without-myspell-dicts        \
             ##--with-system-dicts      \
             ##--with-external-dict-dir=/usr/share/myspell \
	
	# build
	mkdir -p $CWD/${PKG}-install/
	cp -r ../export icon-themes/  ###hack
	make build-nocheck || exit 1
	# nasty hack here
	touch instdir/share/config/images_WITH_WEBDAV=neon.zip
	#touch instdir/share/config/images_export.zip
	cp instdir/share/config/images_tango.zip instdir/share/config/images_\\tango.zip #this needs undoing in package
	# end nasty hack
	make distro-pack-install  || exit 1
	
	# clean ups
	rm -r $CWD/${PKG}-install/usr/$LIBDIR/libreoffice/readmes &&
	rm -r $CWD/${PKG}-install/usr/share/icons &&
	cp -r ../template/icons $CWD/${PKG}-install/usr/share/pixmaps  &&
	cp -r ../template/etc $CWD/${PKG}-install/
	mv $CWD/${PKG}-install/usr/$LIBDIR/libreoffice/share/config/images_\\tango.zip $CWD/${PKG}-install/usr/$LIBDIR/libreoffice/share/config/images_tango.zip
	(
	cd $CWD/${PKG}-install/usr/bin/
	ln -sf ../$LIBDIR/libreoffice/program/soffice libreoffice
	ln -sf ../$LIBDIR/libreoffice/program/soffice soffice
	for z in  localc loffice loimpress loweb lobase lodraw lofromtemplate lomath lowriter unopkg;do
		sed -i "s%^\/.*$%/usr/$LIBDIR/libreoffice/program/$z  "$@"%" $z
	done
	)

	# adjust icons categories as needed
	for app in base calc draw impress math startcenter writer; do
		case $app in
			base|calc) cat="Database;Spreadsheet;" ;;
			draw) cat="VectorGraphics;" ;;
			math) cat="X-Graphic;" ;;
			impress)   cat="Presentation;" ;;
			startcenter|writer) cat="WordProcessor;" ;;
		esac
		sed -i -e "s/^Categories=.*$/Categories=${cat}/" \
				-e 's/ %.*$//' \
				$CWD/${PKG}-install/usr/$LIBDIR/libreoffice/share/xdg/${app}.desktop
		ICN=`grep '^Icon\=' $CWD/${PKG}-install/usr/$LIBDIR/libreoffice/share/xdg/${app}.desktop`
		sed -i "s/$ICN/${ICN}\.png/" $CWD/${PKG}-install/usr/$LIBDIR/libreoffice/share/xdg/${app}.desktop
	done
	# fix symlinks
	for i in `ls $CWD/${PKG}-install/usr/$LIBDIR/libreoffice/share/xdg`; do
		echo linking $i to libreoffice-${i}
		(
		cd $CWD/${PKG}-install/usr/share/applications
		ln -sf ../../$LIBDIR/libreoffice/share/xdg/$i libreoffice-${i}
		)
	done
	# lang and help
	{	
		# package help files
		for p in $CWD/${PKG}-install/usr/$LIBDIR/libreoffice/help/*; do
			if [ -d $p ]; then
				mkdir -p $CWD/${PKG}_help-${p##*/}-${VER}-${ARCH}_${BUILD}/usr/$LIBDIR/libreoffice/help/
				mv -v $p $CWD/${PKG}_help-${p##*/}-${VER}-${ARCH}_${BUILD}/usr/$LIBDIR/libreoffice/help/
			fi
		done
		
		# package dict files
		for p in $CWD/${PKG}-install/usr/$LIBDIR/libreoffice/share/extensions/dict-*; do
			if [ -d $p ]; then
				mkdir -p $CWD/${PKG}_${p##*/}-${VER}-${ARCH}_${BUILD}/usr/$LIBDIR/libreoffice/share/extensions/
				mv -v $p $CWD/${PKG}_${p##*/}-${VER}-${ARCH}_${BUILD}/usr/$LIBDIR/libreoffice/share/extensions/
			fi
		done
		
		# lastly move the main dir
		mv $CWD/${PKG}-install $CWD/${PKG}-${VER}-${ARCH}_${BUILD}/
		cd $CWD
		
		# always successful if we get here
		return 0 
	}
	
}

package_special() {
	# precaution
	[ "`pwd`" = "$CWD" ] || cd $CWD
	# add this recipe
	mkdir -p ${PKG}_DEV-${VER}-${ARCH}_${BUILD}/usr/src/dev
	cat ${PKG}.petbuild > ${PKG}_DEV-${VER}-${ARCH}_${BUILD}/usr/src/dev/${PKG}-build-recipe
	for p in $(ls|grep "\-${ARCH}") ; do
		case $p in
			*_DEV*) DESC="$PKG development"; DEPS=+${PKG} ;;
			*_DOC*) DESC="$PKG documentation"; DEPS=+${PKG} ;;
			*_NLS*) DESC="$PKG locales"; DEPS=+${PKG} ;;
			*help*) lang=${p#*-}; lang=${lang%%-*}
					DESC="$PKG - $lang help files"
			;;
			*dict*) lang=${p#*-}; lang=${lang%%-*}; lang=${lang/dict\-//}
					DESC="$PKG - $lang dictionary files"
			;;
		esac	
		echo "packaging $p"	
		dir2pet -x -s -w="$DESC" -d="$DEPS" -c="$CAT" -p=${p} 2>&1 >/dev/null
		rm -r $p
		mv ${p}.pet ../0pets_out/
	done
	rm -r ${PKG}-${VER}
	echo "done!"
}

# main
retrieve ${PKG}-${VER}.${COMP}
extract ${PKG}-${VER}.${COMP}
retrieve_extras || exit $?
build
package_special
